<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>地址管理</title>
    <meta name="keywords" content="">
    <meta name="description" content="">
    <meta name="renderer" content="webkit">
    <meta http-equiv="Cache-Control" content="no-siteapp"/>
    <meta name="mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,address=no"/>
    <meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no">
    <link rel="stylesheet" href="../css/zy_common.css"/>
    <link rel="stylesheet" href="../css/zy_base.css"/>
    <link rel="stylesheet" href="../js/drop/dropload.css"/>
</head>
<body>
    <div class="addressBox" style="padding-bottom: 1.4rem;">
        <ul>
            <!--<li class="bw mb20">-->
                <!--<div class="plr24 ptb30 bdb font15 col3">-->
                    <!--<p class="flex_sb mb20"><span>勇仔</span><span>15812874182</span></p>-->
                    <!--<p>广东省东莞市南城区宏图路</p>-->
                <!--</div>-->
                <!--<div class="plr24 flex_sb">-->
                    <!--<label class="h100 flex_a col3">-->
                        <!--<input type="radio" name="radio" hidden class="chose_addr" value="1" checked>-->
                        <!--<span class="roudBox border"></span>-->
                        <!--<i class="font15 ml20 color2">默认地址</i>-->
                    <!--</label>-->
                    <!--<div class="flex jce">-->
                        <!--<em class="editBox h100 flex_a"><span class="mr20 col9 font14">编辑</span><img src="../img/edit.png" alt=""></em>-->
                        <!--<em class="delBox h100 flex_a ml40"><span class="mr20 col9 font14">删除</span><img src="../img/del.png" alt=""></em>-->
                    <!--</div>-->
                <!--</div>-->
            <!--</li>-->
            <!--<li class="bw mb20">-->
                <!--<div class="plr24 ptb30 bdb font15 col3">-->
                    <!--<p class="flex_sb mb20"><span>勇仔</span><span>15812874182</span></p>-->
                    <!--<p>广东省东莞市南城区宏图路</p>-->
                <!--</div>-->
                <!--<div class="plr24 flex_sb">-->
                    <!--<label class="h100 flex_a col3">-->
                        <!--<input type="radio" name="radio" hidden class="chose_addr" value="2">-->
                        <!--<span class="roudBox border"></span>-->
                        <!--<i class="font15 ml20">默认地址</i>-->
                    <!--</label>-->
                    <!--<div class="flex jce">-->
                        <!--<em class="editBox h100 flex_a"><span class="mr20 col9 font14">编辑</span><img src="../img/edit.png" alt=""></em>-->
                        <!--<em class="delBox h100 flex_a ml40"><span class="mr20 col9 font14">删除</span><img src="../img/del.png" alt=""></em>-->
                    <!--</div>-->
                <!--</div>-->
            <!--</li>-->
        </ul>
    </div>
    <div class="pos_f_b ptb30 bgf5">
        <a href="./zy_add.html" class="confirm">新增地址</a>
    </div>
</body>
</html>
<script type="text/javascript" src="../js/jquery.min.js"></script>
<script type="text/javascript" src="../js/dlc.js"></script>
<script type="text/javascript" src="../js/drop/dropload.min.js"></script>
<script>
    var page = 0;
    // 每页展示10个
    var size = 10;

    $(function () {
        addEventback();//返回刷新
        //设备默认地址
        $('.addressBox').on('change','.chose_addr',function () {
            var addressId = $('.chose_addr:checked').val();
            if($(this).is(':checked')){
                var that = this;
                dlctipbox.loading('请稍后');
                dlc_request('/api/address/changeAddressStatus',{addressId:addressId,isDefault:1},function (res) {
                    dlctipbox.clear();
                    if(res.code==1){
                        $(that).parent('label').find('i').text('默认地址').addClass('color1').end().parents('li').siblings().find('i').text('设为默认').removeClass('color1');
                    }else{
                        dlctipbox.show(res.msg);
                    }
                })
            }
        });

        //编辑
        $('.addressBox').on('click','.editBox',function () {
            var msg={};
            msg.name=$(this).parents('li').data('name');
            msg.province=$(this).parents('li').data('province');
            msg.addr=$(this).parents('li').data('addr');
            msg.is_default=$(this).parents('li').data('is_default');
            msg.phone=$(this).parents('li').data('phone');
            saveLocalData('addr_data',msg);
            var addressId = $(this).parents('li').find('.chose_addr').val();
            location.href='./zy_add.html?addressId='+addressId;
        });

        //删除
        $('.addressBox').on('click','.delBox',function () {
            var that = this;
            dlctipbox.confirm('是否删除该地址？',function (flag) {
                if(flag==1){
                    var addressId = $(that).parents('li').find('.chose_addr').val();
                    console.log(addressId);
                    dlctipbox.loading('请稍后');
                    dlc_request('/api/address/delete',{addressId:addressId},function (res) {
                        dlctipbox.clear();
                        if(res.code==1){
                            dlctipbox.show('删除成功');
                            setTimeout(function () {
                                location.reload();
                            },1500);
                        }else{
                            dlctipbox.show(res.msg);
                        }
                    })
                }
            })
        });

        $('.addressBox').dropload({
            scrollArea : window,
            domUp : {
                domClass   : 'dropload-up',
                domRefresh : '<div class="dropload-refresh">↓下拉刷新</div>',
                domUpdate  : '<div class="dropload-update">↑释放更新</div>',
                domLoad    : '<div class="dropload-load"><span class="loadings"></span>加载中...</div>'
            },
            domDown : {
                domClass   : 'dropload-down',
                domRefresh : '<div class="dropload-refresh">↑上拉加载更多</div>',
                domLoad    : '<div class="dropload-load"><span class="loadings"></span>加载中...</div>',
                domNoData  : '<div class="dropload-noData">暂无更多数据</div>'
            },
            loadUpFn : function(me){
                var str = '';
                dlc_request('/api/address/list',{page:1,limit:size},function (res) {
                    console.log(res);
                    if(res.code==1){
                        res.data.list.forEach(function (item,index) {
                            str+= '<li class="bw mb20" data-phone="'+item.phone+'" data-name="'+item.name+'" data-province="'+item.province+'" data-addr="'+item.addr+'" data-is_default="'+item.isDefault+'">'+
                                '<div class="plr24 ptb30 bdb font15 col3">'+
                            '<p class="flex_sb mb20"><span>'+item.name+'</span><span>'+item.phone+'</span></p>'+
                            '<p>'+(item.province).replace(/,/g,'')+item.addr+'</p>'+
                            '</div>'+
                            '<div class="plr24 flex_sb">'+
                                '<label class="h100 flex_a col3">'+
                                '<input type="radio" name="radio" hidden class="chose_addr" value="'+item.addressId+'" '+(item.isDefault==0?'':'checked')+'>'+
                                '<span class="roudBox border"></span>'+
                                '<i class="font15 ml20 '+(item.isDefault==0?'':'color1')+'">'+(item.isDefault==0?'设为默认':'默认地址')+'</i>'+
                                '</label>'+
                                '<div class="flex jce">'+
                                '<em class="editBox h100 flex_a"><span class="mr20 col9 font14">编辑</span><img src="../img/edit.png" alt=""></em>'+
                            '<em class="delBox h100 flex_a ml40"><span class="mr20 col9 font14">删除</span><img src="../img/del.png" alt=""></em>'+
                            '</div>'+
                            '</div>'+
                            '</li>';
                        });
                        $('.addressBox ul').html(str);
                        // 每次数据加载完，必须重置
                        me.resetload();
                        // 重置页数，重新获取loadDownFn的数据
                        page = 1;
                        // 解锁loadDownFn里锁定的情况
                        me.unlock();
                        me.noData(false);
                    }
                });
            },
            loadDownFn : function(me){
                page++;
                if(page==1){
                    dlctipbox.loading('载入中');
                }
                // 拼接HTML
                var str = '';
                var data={page:page,limit:size};
                dlc_request('/api/address/list',data,function (res) {
                    dlctipbox.clear();
                    if(res.code==1){
                        var arrLen = res.data.list.length;
                        if(arrLen > 0){
                            res.data.list.forEach(function (item,index) {
                                str+= '<li class="bw mb20" data-phone="'+item.phone+'" data-name="'+item.name+'" data-province="'+item.province+'" data-addr="'+item.addr+'" data-is_default="'+item.isDefault+'">'+
                                    '<div class="plr24 ptb30 bdb font15 col3">'+
                                    '<p class="flex_sb mb20"><span>'+item.name+'</span><span>'+item.phone+'</span></p>'+
                                    '<p>'+(item.province).replace(/,/g,'')+item.addr+'</p>'+
                                    '</div>'+
                                    '<div class="plr24 flex_sb">'+
                                    '<label class="h100 flex_a col3">'+
                                    '<input type="radio" name="radio" hidden class="chose_addr" value="'+item.addressId+'" '+(item.isDefault==0?'':'checked')+'>'+
                                    '<span class="roudBox border"></span>'+
                                    '<i class="font15 ml20 '+(item.isDefault==0?'':'color1')+'">'+(item.isDefault==0?'设为默认':'默认地址')+'</i>'+
                                    '</label>'+
                                    '<div class="flex jce">'+
                                    '<em class="editBox h100 flex_a"><span class="mr20 col9 font14">编辑</span><img src="../img/edit.png" alt=""></em>'+
                                    '<em class="delBox h100 flex_a ml40"><span class="mr20 col9 font14">删除</span><img src="../img/del.png" alt=""></em>'+
                                    '</div>'+
                                    '</div>'+
                                    '</li>';
                            });
                            // 如果没有数据
                        }else{
                            // 锁定
                            me.lock();
                            // 无数据
                            me.noData();
                        }
                        // 插入数据到页面，放到最后面
                            $('.addressBox ul').append(str);

                        // 每次数据插入，必须重置
                        me.resetload();
                    }
                });
            },
            threshold : 50
        });
    })

</script>